package core.proto.rightwindow;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import com.oreilly.servlet.HttpMessage;

import core.proto.InfoViz;
import core.proto.Util;

public class WebUtil implements MessageTypes {

	public final static String SERVER_ABS_URL = "http://" + InfoViz.codebase().getAuthority() + "/ChatServlet/chatservlet"; 

	public static void sendChatMessage (String message) throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(CONTENT, message);
		props.put(MSG_TYPE_KEY, CHAT_TYPE);
		props.put(ACTION_TYPE, CHAT_PUSH);
		
		msg.sendPostMessage(props); // Post message for chat.
	}

	public static InputStream pullChatMessage () throws IOException {
		URL url;
		url = new URL(SERVER_ABS_URL);
		HttpMessage msg = new HttpMessage(url);
		Properties props = new Properties(); 
		props.put(MSG_TYPE_KEY, CHAT_TYPE);
		props.put(ACTION_TYPE, CHAT_PULL);
		return msg.sendPostMessage(props);
	}

	public static void sendPostIt (String message) throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		
		Properties props = new Properties();
		props.put(MSG_TYPE_KEY, MessageTypes.ANNO_TYPE);
		props.put(ACTION_TYPE, MessageTypes.ANNO_PUSH);
		props.put(CONTENT, message);
		
		msg.sendPostMessage(props);
	}

	public static InputStream pullPostItUpdates () throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		Properties props = new Properties();
		props.put(MSG_TYPE_KEY, MessageTypes.ANNO_TYPE);
		props.put(ACTION_TYPE, MessageTypes.ANNO_PULL);
		
		return msg.sendPostMessage(props);
	}
}
